package com.fau.cm.action.index;

import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCallSMSPhrase;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc  常用短语设置Action
 * @author yangzj
 * @datetime 2014年10月19日,下午4:28:20
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class SmsCommonSettingsAction extends PublicAction implements ModelDriven<MedicalCallSMSPhrase>{
	
	private static final long serialVersionUID = 7468300004896446073L;
	
	private MedicalCallSMSPhrase phrase = new MedicalCallSMSPhrase();

	/**
	 * @desc 跳转到常用短语设置页面
	 * @return SUCCESS
	 * @author yangzj
	 * @datetime 2014年10月19日,下午4:35:42
	 */
	@Action(value = "sms_common_settings_init",results = {@Result(location = "sms_common_settings_init.jsp")})
	public String init() {
		return SUCCESS;
	}
	
	/**
	 * @desc 跳转到常用短语添加页面
	 * @return SUCCESS
	 * @author yangzj
	 * @datetime 2014年10月20日,上午10:12:35
	 */
	@Action(value = "sms_phrase_add",results = {@Result(location = "sms_phrase_add.jsp")})
	public String add() {
		return SUCCESS;
	}
	
	/**
	 * @desc 跳转到常用短语修改页面
	 * @return SUCCESS
	 * @author yangzj
	 * @datetime 2014年10月20日,上午10:12:35
	 */
	@Action(value = "sms_phrase_edit",results = {@Result(location = "sms_phrase_edit.jsp")})
	public String edit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 分页查询常用短语设置
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月19日,下午6:27:16
	 */
	@Action(value = "sms_common_settings_view")
	public String view() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getSmsService().findSmsSettingsByPage(admin, phrase, map);
		this.map.clear();
		this.map.put("total", this.pageBean.getTotalRow());
		this.map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}

	/**
	 * @desc 保存添加
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月20日,上午11:10:35
	 */
	@Action(value = "sms_phrase_save_add")
	public String saveAdd(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Boolean flag =  this.baseAllService.getSmsService().saveAdd(admin, phrase);
		if(flag){
			this.map.put("success", true);
		}else{
			this.map.put("success", false);
		}
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 保存修改
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月20日,上午10:49:53
	 */
	@Action(value = "sms_phrase_save_edit")
	public String saveEdit(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(phrase.getId())){
			this.baseAllService.getSmsService().editSave(phrase);
			this.map.put("success", true);
		}else{
			this.map.put("error", true);
		}
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 删除常用短语
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月20日,上午11:30:31
	 */
	@Action(value = "sms_phrase_delete")
	public String del(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(phrase.getIds())){
			this.baseAllService.getSmsService().deletePhrase(phrase.getIds());
			this.map.put("success", true);
		}else{
			this.map.put("error", true);
		}
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 根据类型查询常用短语
	 * @return null
	 * @author liy
	 * @datetime 2015年1月9日,下午4:54:57
	 */
	@Action(value = "sms_phrase_by_type")
	public String byType(){
		HttpServletResponse response = ServletActionContext.getResponse();
		if(StringUtils.isNotBlank(phrase.getType())){
			this.list = this.baseAllService.getSmsService().findSmsPhraseByType(phrase.getType());
			formatJSON(response, this.list);
		}
		return null;
	}
	
	@Override
	public MedicalCallSMSPhrase getModel() {
		return phrase;
	}
}
